package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.*;
import com.woniuxy.mapper.DoctorMapper;
import com.woniuxy.service.IDoctorService;
import com.woniuxy.service.SaasService;
import com.woniuxy.util.CONSTANT;
import com.woniuxy.util.GetUserContent;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author：xurice
 * @date：Created in 2024/4/12 15:00
 * @description：
 * @modified By：
 * @version: $
 */
@Service
public class DoctorServiceImpl implements IDoctorService , SaasService {
    @Autowired
    DoctorMapper doctorMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    //查询挂号信息
    @SneakyThrows
    @Override
    public ResponseData<?> selectRegistered(PageParam<Registered> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        Map userInfo = GetUserContent.getUserInfo();
        Long userId = (Long) userInfo.get(CONSTANT.USER_ID);
        pageParam.getParam().setRsUserId(userId.intValue());
        List<Registered> registereds = doctorMapper.selectRegistered(pageParam.getParam());
        PageInfo<Registered> pageInfo = new PageInfo<>(registereds);
        return new ResponseData<>().ok(pageInfo);
    }



    //查询当前医生信息
    @SneakyThrows
    @Override
    public ResponseData<?> queryDoctor() {
        Map userInfo = GetUserContent.getUserInfo();
        Long userId = (Long) userInfo.get(CONSTANT.USER_ID);
        Doctor doctor = (Doctor) redisTemplate.opsForValue().get(CONSTANT.DOCTOR + userId);
        if (doctor == null) {
            doctor = doctorMapper.queryDoctor(userId.intValue());
            redisTemplate.opsForValue().set(CONSTANT.DOCTOR + userId, doctor, 30, TimeUnit.DAYS);
        }
        return new ResponseData<>().ok(doctor);
    }

    //查出全部检查
    @Override
    public ResponseData<?> queryChecks() {
        Doctor doctor = (Doctor) queryDoctor().getData();
        List<Check> checks = (List<Check>) redisTemplate.opsForHash().get(CONSTANT.CHECK, ""+doctor.getId());
        if (checks == null) {
            checks = doctorMapper.queryChecks(doctor);
            redisTemplate.opsForHash().put(CONSTANT.CHECK, ""+doctor.getId(), checks);
        }
        return new ResponseData<>().ok(checks);
    }

    @Override
    public ResponseData<?> queryPatient(Integer id) {
        Patient patient = (Patient) redisTemplate.opsForValue().get(CONSTANT.PATIENT + id);
        if (patient == null) {
            patient = doctorMapper.queryPatient(id);
            redisTemplate.opsForValue().set(CONSTANT.PATIENT + id, patient, 30, TimeUnit.DAYS);
        }
        return new ResponseData<>().ok(patient);
    }
}
